<?php
class Integral{
	
	public $num; //总积分
	public $name; //积分名称
	public $member_id; //用户ID
	public $product = array(); //商品集合
	public $confirm = false;   //是否抵现
	public $dao;
	public $isPay = false;
	public function Integral($member_id){
	
		$this->member_id = $member_id;
		$this->initialize();
	}
	
	
    //初始化积分
	public function initialize(){
	   $memberDB = M('member');
	   $integral_numberDB = M('integral_number');
	   $integral_typeDB = M('integral_type');
	   $member = $memberDB->where(array('id'=>$this->member_id))->find();
	   $integral_type = $integral_typeDB->where(array('site_id'=>$member['site_id']))->select();
	   $integral_number = $integral_numberDB->where(array('member_id'=>$this->member_id))->select();
	   if($integral_number){
	     return false;

	   }
	   foreach($integral_type as $key=>$value){
	      $data = array(
		     'type_id'=>$value['id'],
			 'number'=>$value['initVal'],
			 'typename'=>$value['typename'],
			 'site_id'=>$value['site_id'],
		     'member_id'=>$this->member_id,
			 'remark'=>$value['remark']
		  );
		   
		   $integral_numberDB->add($data);
	   }
	   return true;
	}
	//查询是否有设置积分帐户，有则false，无则添加帐户并返回结果
	public function addIntegralNum($memberid){
		$memberid=$memberid?$memberid:$this->member_id;
		$integral_numberDB = M('Integral_number');
		$integral_typeDB=M('Integral_type');
		$integral_logdb = M('Integral_log');
		$integralrankingdb = M('Integralranking');
		$memberDB=M('Member');
		$memberData=$memberDB->where(['id'=>$memberid])->find();
		$typeData=$integral_typeDB->where(['site_id'=>__SITEID__])->select();
		$numbCoun=$integral_numberDB->where(['member_id'=>$memberid])->count();
		if(count($typeData)<=$numbCoun){
			return true;
		}else{
			//循环遍历当前站点的积分类型，并添加没有的类型
			foreach($typeData as $k=>$v){
				if($integral_numberDB->where(['type_id'=>$v['id'],'member_id'=>$memberid])->find()){
					continue;	
				}
				$data=$v;					  //直接使用积分类型的 类名+备注+站点ID
				$data['type_id']=$v['id'];	  //积分类型ID
				$data['member_id']=$memberid; //用户ID
				$data['number']=$v['initVal'];//起始分数
				unset($data['id']);
				$ret=$ret?$integral_numberDB->data($data)->add():$ret;
				
				//创建积分事件
				$data['changenum']=$data['totalnum']=$data['number'];
				$data['integralname']=$data['remark'];
				$data['remark']='帐号创建成功并加分'.$data['number'];
				$data['approvername']='系统';
				$data['time']=time();
				$data['member_name']=$memberData['name'];
				$data['memberpositionid']=$memberData['memberpositionid'];
				$data['memberpositionname']=$memberData['memberpositionname'];
				$data['site_id']=__SITEID__;
				$inteRet=$integral_logdb->data($data)->add();
				//并添加至积分排名表
				$data['integraltypeid']=$data['type_id'];
				$data['integrallogid']=$inteRet;
				$data['dateline']=$data['time'];
				$data['total']=$data['changenum'];
				$rankRet=$integralrankingdb->data($data)->add();
			}	
		}
		return $ret;
	}
	//查询用户是否有积分帐户
	public function checkIntegralNum($memberid){
		$memberid=$memberid?$memberid:$this->member_id;
		$integral_numberDB = M('Integral_number');
		$integral_typeDB=M('Integral_type');
		$typeData=$integral_typeDB->where(['site_id'=>__SITEID__])->select();
		$numbCoun=$integral_numberDB->where(['member_id'=>$memberid])->count();
		if(count($typeData)<=$numbCoun){
			return true;
		}else{
			return false;
		}
	}
    //添加分数，日志，排名
	public function addIntegral($event,$mission){
		$Integral_numberdb = M('Integral_number');
		$Integral_logdb = M('Integral_log');
		$memberdb=M('Member');
		//加上积分账户
		$this->addIntegralNum($event['member_id']);
		
		
		
		
		//将分数加至积分账户
		$Integral_numberdb -> where(array('site_id' => __SITEID__, 'member_id' => $event['member_id'], 'type_id' => $event['integraltypeid'])) -> setInc('number', $event['integralnumber']);
		$integrallog['totalnum'] = (float)$Integral_numberdb -> where(array('type_id' => $event['integraltypeid'], 'site_id' => __SITEID__, 'member_id' => $event['member_id'])) -> getField('number');
		$integrallog['memberpositionname'] = $memberdb->where(['id'=>$event['member_id']])->getField('memberpositionname');
		$integrallog['membername'] = 		 $event['membername'];
		$integrallog['rule_id'] = 		 	 $mission['rule_topid'];
		$integrallog['approvername'] = 		 $mission['approvername'];
		$integrallog['type_id'] = 			 $event['integraltypeid'];
		$integrallog['remark'] = 			 $event['remark'];
		$integrallog['changenum'] = 		 $event['integralnumber'];
		$integrallog['member_id'] = 		 $event['member_id'];
		$integrallog['site_id'] = 			 __SITEID__;
		$integrallog['event_id'] = 			 $event['id'];
		$integrallog['form_id'] = 			 $mission['form_id'];
		$integrallog['mission_id'] = 		 $mission['id'];
		$integrallog['time'] = 				 $event['datetime'];
		$integrallog['giver_id'] = 			 $this->member_id;
		$integrallog['rewarder_id'] = 		 $this->member_id;
		//加至积分日志
		$ret=$Integral_logdb -> add($integrallog);
		//加至积分排名
		D('Integralranking') -> getDataFromLog($integrallog);	
		
		return $ret;
	}
	
	//更新积分信息,提交到数据库
    public function setIntegral($data,$dao){
	     
		$integral_numberDB = M('integral_number');
		$member_id = $this->member_id;
		$mes = $integral_numberDB->where(array('member_id'=>$member_id,'typename'=>$dao))->save($data);
         
	    if($mes){
		   
		   return true;
		 
		 }else{
		   
		   return false;
		 
		 }		
		
	}
	
	//计算积分数量
    public function giveIntegral($num){
	  $memberDB = M('member');
	  $integral_numberDB = M('integral_number');
	  $integral_typeDB = M('integral_type');   
	  $datenum = $integral_numberDB->where(array('member_id'=>$this->member_id))->select();
	  $data = array();
	  foreach($datenum as $key=>$value){
		  
		   $integral_type = $integral_typeDB->where(array('id'=>$value['type_id']))->find();
		   $number = $datenum[$key]['number']+$num*$integral_type['typeratio'];
		   $mes = $integral_numberDB->where(array('id'=>$datenum[$key]['id']))->save(array('number'=>$number));
		   if($mes){
			  $remark = '赠送'.$value['remark'].'数量为:'.$num*$integral_type['typeratio'].' 目前总数为：'.$number;
			  
			  sendWXMsg($memberDB -> where(array('id' => $this->member_id)) -> getField('openid'), $remark);
		      $this->integral_logs($remark,$num);
			}
	   }
	  return true;
	}
	
	//更详细地计算积分数量
    public function giveIntegralMore($num,$ruleid,$memberid,$remark=''){
	  $memberDB = M('member');
	  $integral_numberDB = M('integral_number');
	  $integral_typeDB = M('integral_type');   
	  $datenum = $integral_numberDB->where(array('member_id'=>$memberid))->select();
	  $data = array();
	  foreach($datenum as $key=>$value){
		  $integral_type = $integral_typeDB->where(array('id'=>$value['type_id']))->find();
		   $number = $datenum[$key]['number']+$num*$integral_type['typeratio'];
		   $mes = $integral_numberDB->where(array('id'=>$datenum[$key]['id']))->save(array('number'=>$number));
		   if($mes){
			  $remark = $remark==''?'赠送'.$value['remark'].'积分:'.$number:$remark;
			  //sendWXMsg($memberDB -> where(array('id' => $this->member_id)) -> getField('openid'), $remark);
		      $this->integral_logMore($member_id,$remark,$num,$value['type_id'],$this->member_id,$ruleid);
			}

	   }
      
	  return true;
	}
	//计算积分数量
    /*public function giveIntegrals($num){
	  $memberDB = M('member');
	  $integral_numberDB = M('integral_number');
	  $integral_typeDB = M('integral_type');   
	  $datenum = $integral_numberDB->where(array('member_id'=>$this->member_id))->select();
	  $data = array();
	  foreach($datenum as $key=>$value){
		   $integral_type = $integral_typeDB->where(array('id'=>$value['type_id']))->find();
		   $$integral_type['typename'] = $num*$integral_type['typeratio'];
		   $data[$integral_type['typename']] = $$integral_type['typename'];
	   }
	  
	  return $data; 
	
	}*/
	
	
	
	
	//获取积分
	public function getIntegral(){
	    
	  $integral_numberDB = M('integral_number'); 
	  $data = $integral_numberDB->where(array('member_id'=>$this->member_id))->select();
	    
	  return $data; 
		
	
	}
	
	
	//积分互转
	/*
	   
	  $mid 给予积分的人
	  $toid 接收积分的人
	  
	*/
	
	//计算抵现
	public function integral_pay_array(){
		
	   return false;
	}
	
	public function adjustIntegral(){
		
	   return false;
	}
	
	//互转资金
	public function adjustMoney(){
		
	   return false;
	}
	
	//判断是否可以互转
	public function judgeIntegral(){
	    return false;
	}
	
	//生成积分日志
	/*
	   remark 原因
	   changenum 金额
	*/
	public function integral_log($member_id,$remark,$changenum){
	   $data['remark'] = $remark;
	   $data['changenum'] = $changenum;
	   $data['member_id'] = $member_id;
	   $data['time']  = time();
	   $integral_logDB = M('integral_log');
	   $integral_logDB->add($data);
	}
	
	//生成积分日志
	/*
	   remark 原因
	   changenum 金额
	*/
	public function integral_logs($remark,$changenum){
	   $data['remark'] = $remark;
	   $data['changenum'] = $changenum;
	   $data['member_id'] = $this->member_id;
	   $data['time']  = time();
	   $integral_logDB = M('integral_log');
	   
	   
	   
	   
	   
	   
	   
	   $integral_logDB->add($data);
	}
	
	//生成积分日志
	/*
	   remark 原因
	   changenum 金额
	*/
	public function integral_logMore($member_id,$remark,$changenum,$type_id,$giver_id,$rule_id){
	   $data['remark'] =$remark;
	   $data['type_id']=$type_id;
	   $data['giver_id']=$giver_id;
	   $data['rule_id']=$rule_id;
	   $data['changenum'] = $changenum;
	   $data['member_id'] = $member_id;
	   $data['time']  = time();
	   $integral_logDB = M('integral_log');
	   $integral_logDB->add($data);
	}
}